package com.common
{
	import com.vo.AttributeVo;
	import com.vo.ClassVo;
	import com.vo.TemplateInfo;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;

	public class Global
	{
		public function Global()
		{
			
		}
		
		[Bindable]
		public static var isSelectAll:Boolean=false;
		

		public static var Log:Function;
		public static var Error:Function;
		
		/**
		 * 转换英文首字母大写
		 * */
		public static function firstCharToUpper(str:String):String
		{
			var firstStr:String = str.substr(0,1);
			var secondStr:String = str.substring(1);

			var changeStr:String =firstStr.toLocaleUpperCase() +secondStr;
			return changeStr;
		}
		
		/**
		 * XML转换成VO
		 */		
		public static function GetProtoList(xml:XML):Vector.<ClassVo>{
			var dataList:Vector.<ClassVo> = new Vector.<ClassVo>();
			for each(var proto:XML in xml.proto){
				if(proto.@ignore == "1")continue;
				var cvo:ClassVo = new ClassVo();
				cvo.className = proto.@name;
				cvo.classType = proto.@type;
				cvo.desc = proto.@desc;
				cvo.mid = int(proto.@mid);
				cvo.module = xml.@name;
				cvo.attributeList = new Vector.<AttributeVo>();
				cvo.isSelect = false;
				for each(var attr:XML in proto.attribute){
					var pvo:AttributeVo = new AttributeVo();
					pvo.attrName = attr.@name;
					pvo.attrType = attr.@type;
					pvo.annotation = attr.@desc;
					pvo.defaultVal = attr.@defa;
					cvo.attributeList.push(pvo);
				}
				dataList.push(cvo);
			}
			return dataList;
		}
		
		
		public static function BuildConst(constXML:XML):void
		{
			var csTempStr:String = Global.template.item.(@name == "cs_const");
			
			var luaTempStr:String = "--此文件由协议工具自动生成，请勿编辑\n\nFieldType =\n{\n";
			var nameDic:Dictionary = new Dictionary();
			var valueDic:Dictionary = new Dictionary();
			
//				<item type="const" name="" value="" desc=""/>

			var as3Content:String = "";
			
			for each(var item:XML in constXML.item)
			{
				var name:String = item.@name;
				var value:String = item.@value;
				
				var luaItemStr = name+" = "+value+",";
				luaTempStr += "\t"+luaItemStr;
				
				var luaSpacingLen:int = 40-luaItemStr.length;
				
				var luaSpacingStr:String = "";
				while(--luaSpacingLen > -1)
				{
					luaSpacingStr+=" ";
				}
				
				var desc:String = item.@desc;
				if(desc && desc != "" && desc != null)
				{
					as3Content+="\t\t///<summary>" + desc + "</summary>\n";
					luaTempStr+=luaSpacingStr+"--"+desc+"\n";
				}
				else
				{
					luaTempStr +="\n";
				}
				as3Content+="\t\tpublic const int "+name+" = "+ value+";\n\n";
			}
			
			luaTempStr +="}";
			
			csTempStr = csTempStr.replace(/\$Attrs/g, as3Content);
			Global.writeFile(Global.ClientProjectPath, "FieldType", csTempStr, "cs");
			
			Global.writeFile(Global.ServerProjectPath,"fieldtype", luaTempStr, "lua");
			
		}

		
		public static function BuildMSG(midXML:XML):void
		{
			var csTempStr:String = Global.template.item.(@name == "cs_cmd");

			var luaTempStr:String = "--此文件由协议工具自动生成，请勿编辑\n\nProtocolCode =\n{\n";
			
			var nameDic:Dictionary = new Dictionary();
			var midDic:Dictionary = new Dictionary();
			
			var as3Content:String = "";
			for each(var item:XML in midXML.item)
			{
				if(item.@type == "br")
				{
					luaTempStr += "\n";
					as3Content+= "\n";
					continue;
				}
				if(item.@type == "desc")
				{
					luaTempStr += "--"+item+"\n";
					as3Content+= "//"+item+"\n";
					continue;
				}
				
				var itemName:String = item.@name;
				var itemMid:String = item.@mid;
				
				if(!nameDic[itemName])
				{
					nameDic[itemName] = true;
				}
				else
				{
					Alert.show("重复的协议名："+itemName, "错误提示");
					Global.Error("重复的协议名："+itemName+"\n");
					return;
				}
				
				if(!midDic[itemMid])
				{
					midDic[itemMid] = true;
				}
				else
				{
					Alert.show("重复的协议ID："+itemMid, "错误提示");
					Global.Error("重复的协议ID："+itemMid+"\n");
					return;
				}
				
				var luaItemStr = itemName+" = "+itemMid+",";
				luaTempStr += "\t"+luaItemStr;
				
				var luaSpacingLen:int = 60-luaItemStr.length;
				
				var luaSpacingStr:String = "";
				while(--luaSpacingLen > -1)
				{
					luaSpacingStr+=" ";
				}
				
				var desc:String = item.@desc;
				if(desc && desc != "" && desc != null)
				{
					as3Content+="\t\t///<summary>" + desc + "</summary>\n";
					luaTempStr+=luaSpacingStr+"--"+desc+"\n";
				}
				else
				{
					luaTempStr +="\n";
				}
				as3Content+="\t\tpublic const int MSG_"+itemName+" = "+ itemMid+";\n\n";
			}
			
			luaTempStr +="}";
			
			csTempStr = csTempStr.replace(/\$Attrs/g, as3Content);
			Global.writeFile(Global.ClientProjectPath, "CMD", csTempStr, "cs");
			
			var moduleDir:File = new File(ProtoProjectPath+"/module");
			var moduleFileStr:String = "--此文件由协议工具自动生成，请勿编辑\n\n";
			if(moduleDir.exists)
			{
				var modules:Array = moduleDir.getDirectoryListing();
				for each (var file:File in modules) 
				{
					moduleFileStr+="require \"protocol/msg/"+ file.name.split(".")[0] +"\"\n";
				}
			}
			
			Global.writeFile(Global.ServerProjectPath, "modulehead", moduleFileStr, "lua");
			
			Global.writeFile(Global.ServerProjectPath,"protocolcode", luaTempStr, "lua");
		}
		
		public static var rootPath:String;
		
		public static var template:XML;
		/** proto工程目录 **/
		public static var ProtoProjectPath:String;
		/**
		 * 服务端工程目录 
		 */		
		public static var ServerProjectPath:String;
		/**
		 * 客户端工程目录 
		 */		
		public static var ClientProjectPath:String;

		public static const CACHE_PROTO_PATH_KEY = "CACHE_PROTO_PATH_KEY";
		
		public static const CACHE_Server_PATH_KEY = "CACHE_Server_PATH_KEY";

		public static const CACHE_Client_PATH_KEY = "CACHE_Client_PATH_KEY";

		
		public static function writeFile(path:String, fileName:String,content:String,type:String):void
		{
			if(!path)return;
			
			var file:File = new File(path+"/"+fileName+"."+type);
			var fileStream:FileStream = new FileStream();
			fileStream.openAsync(file, FileMode.WRITE);
			fileStream.writeUTFBytes(content);
			fileStream.close();
		}
		
//		public static function deleteFile(fileName:String,type:String):void
//		{
//			var file:File = new File(ProtoProjectPath+"/"+fileName+"."+type);
//			if(file && file.exists)
//			{
//				file.deleteFile();
//			}
//		}
//		
//		public static function appendFile(fileName:String,content:String,type:String):void
//		{
//			var file:File = new File(ProtoProjectPath+"/"+fileName+"."+type);
//			var fileStream:FileStream = new FileStream();
//			fileStream.openAsync(file, FileMode.APPEND);
//			fileStream.writeUTFBytes(content);
//			fileStream.close();
//		}
		
	}
}